package mq

import (
	"errors"
	"strings"

	"lottery/conf"
)

type MessageQueue interface {
	Destroy()
	ReceiveStringChan(string) <-chan string
	MultiReceivTopic(string, []string) <-chan *Msg // 使用该方法可以避免在同一进程内出现多个app接受同一个topic
	SendString(string, string) error
}

type Msg struct {
	Topic string
	Value string
}

func OpenKafkaMQ(name string) (mq MessageQueue, err error) {
	res := conf.LoadResource()
	cfg, ok := res.Kafka[name]
	if !ok {
		err = errors.New("找不到配置")
		return
	}

	hosts := strings.Split(cfg.Host, ",")
	mq = NewKafkaClusterMQ(hosts, res.KafkaGroup, false)
	return
}

func OpenBaseKafkaMQ() (MessageQueue, error) {
	return OpenKafkaMQ("base")
}
